package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    private final CategoryBrandService categoryBrandService;
    public BrandServiceImpl(CategoryBrandService categoryBrandService) {
        this.categoryBrandService = categoryBrandService;
    }

    //根据分类id查询品牌
    @Override
    public List<Brand> queryBrandByCategory(Long id) {
        return getBaseMapper().queryBrandByCategory(id);
    }

    @Override
    public PageDTO<BrandDTO> queryBrandByPage(Integer page, Integer rows, String key) {
        //引入工具org.apache.commons.lang3.StringUtils进行非空判断
        boolean isKeyExists = StringUtils.isNoneBlank(key);
        Page<Brand> brandPage = query().like("name", key)
                .or()
                .like(isKeyExists,"letter", key)
                .page(new Page<>(page, rows));
        //List<Brand> brandList = brandPage.getRecords();
        return new PageDTO<BrandDTO>(brandPage.getTotal(),brandPage.getPages(),BrandDTO.convertEntityList(brandPage.getRecords()));
    }

    @Override
    public void saveBrand(BrandDTO brandDTO) {
        //添加品牌
        Brand brand = brandDTO.toEntity(Brand.class);
        save(brand);
        //添加类别，品牌中间表
        //使用id来构建中间表list集合
        List<CategoryBrand> categoryBrandList = brandDTO.getCategoryIds().stream()
                .map(id -> CategoryBrand.of(id, brand.getId()))
                .collect(Collectors.toList());
        //保存中间表
        categoryBrandService.saveBatch(categoryBrandList);
    }

    //更新品牌
    @Override
    @Transactional
    public void updateBrand(BrandDTO brandDTO) {
        //删除中间表数据：
        categoryBrandService.remove(new QueryWrapper<CategoryBrand>().eq("brand_id",brandDTO.getId()));
        //更新表数据
        updateById(brandDTO.toEntity(Brand.class));
        //重新插入中间表数据
        List<CategoryBrand> categoryBrandList = brandDTO.getCategoryIds().stream()
                .map(id -> CategoryBrand.of(id, brandDTO.getId())).collect(Collectors.toList());
        categoryBrandService.saveBatch(categoryBrandList);
    }

    @Override
    @Transactional
    public void removeBrandById(Long id) {
        //删除品牌表
        removeById(id);
        //删除中间表
        categoryBrandService.remove(new QueryWrapper<CategoryBrand>().eq("brand_id",id));
    }

    //用户登录名称 leyou@1134704927995639.onaliyun.com
    //AccessKey ID LTAI4GKhzN1L6TCNw9RRaddP
    //AccessKey Secret D8hM3rL6pI3YiKzGJTiMt2CA1UU11z
}